import { CALL_API } from 'redux-api-middleware'
import cookie from 'react-cookie'
import { extend, isUndefined } from 'lodash/fp'
var _ = require('underscore')

import config from 'config'

// Constants
export const LOGIN_CODE_REQUEST = 'LOGIN_CODE_REQUEST'
export const LOGIN_CODE_SUCCESS = 'LOGIN_CODE_SUCCESS'
export const LOGIN_CODE_FAILURE = 'LOGIN_CODE_FAILURE'

export const REG_CODE_REQUEST = 'REG_CODE_REQUEST'
export const REG_CODE_SUCCESS = 'REG_CODE_SUCCESS'
export const REG_CODE_FAILURE = 'REG_CODE_FAILURE'

export const LOGIN_STATUS_REQUEST = 'LOGIN_STATUS_REQUEST'
export const LOGIN_STATUS_SUCCESS = 'LOGIN_STATUS_SUCCESS'
export const LOGIN_STATUS_FAILURE = 'LOGIN_STATUS_FAILURE'

export const SINGOUT_REQUEST = 'SINGOUT_REQUEST'
export const SINGOUT_SUCCESS = 'SINGOUT_SUCCESS'
export const SINGOUT_FAILURE = 'SINGOUT_FAILURE'

// Actions
// 注册/登录
export function singUp (mobile = '', code = '', key = '') {
  return {
    [CALL_API]: {
      endpoint: config.api_root_v1 + '/user.php?c=user',
      method: 'POST',
      credentials: "include",
      body: JSON.stringify({
        "jsonrpc": "2.0",
        "method": "singUp",
        "params": [{
          "phone": mobile,
          "sms_code": code,
          "key": key
        }],
        "id": 1
      }),
      types: [
        LOGIN_CODE_REQUEST,
        {
          type: LOGIN_CODE_SUCCESS,
          payload: (action, state, res) => {
            return res.json()
          }
        },
        {
          type: LOGIN_CODE_FAILURE,
          payload: (action, state, res) => {
            return res.json()
          }
        }
      ]
    }
  }
}

// 请求短信验证码
export function regCode (mobile = '') {
  return {
    [CALL_API]: {
      endpoint: config.api_root_v1 + '/user.php?c=user',
      method: 'POST',
      credentials: "include",
      body: JSON.stringify({
        "jsonrpc": "2.0",
        "method": "messageAuth",
        "params": [
          {
            "phone": mobile,
            "node_name": "singup"
          }
        ],
        "id": 1
      }),
      types: [
        REG_CODE_REQUEST,
        {
          type: REG_CODE_SUCCESS,
          payload: (action, state, res) => {
            return res.json()
          }
        },
        {
          type: REG_CODE_FAILURE,
          payload: (action, state, res) => {
            return res.json()
          }
        }
      ]
    }
  }
}

// 验证登录状态
export function loginStatus (mobile = '') {
  return {
    [CALL_API]: {
      endpoint: config.api_root_v1 + '/user.php?c=user',
      method: 'POST',
      body: JSON.stringify({
        jsonrpc: '2.0',
        method: 'loginStatus',
        params: [],
        id: 1
      }),
      credentials: "include",
      types: [
        LOGIN_STATUS_REQUEST,
        {
          type: LOGIN_STATUS_SUCCESS,
          payload: (action, state, res) => {
            return res.json()
          }
        },
        {
          type: LOGIN_STATUS_FAILURE,
          payload: (action, state, res) => {
            return res.json()
          }
        }
      ]
    }
  }
}

// 退出登录
export function logout () {
  return {
    [CALL_API]: {
      endpoint: config.api_root_v1 + '/user.php?c=user',
      method: 'POST',
      body: JSON.stringify({
        "jsonrpc": "2.0",
        "method": "singOut",
        "params": [],
        "id": 1
      }),
      credentials: "include",
      types: [
        SINGOUT_REQUEST,
        {
          type: SINGOUT_SUCCESS,
          payload: (action, state, res) => {
            return res.json()
          }
        },
        {
          type: SINGOUT_FAILURE,
          payload: (action, state, res) => {
            return res.json()
          }
        }
      ]
    }
  }
}

export const actions = {
  singUp,
  regCode,
  logout,
  loginStatus
}

// Action Handlers
const ACTION_HANDLERS = {
  [LOGIN_CODE_REQUEST]: (state, action) => {
    return ({ ...state, error: {}, cookie: false, signed: false, singUp: {}, isFetching: true })
  },
  [LOGIN_CODE_SUCCESS]: (state, action) => {
    cookie.save('token', action.payload.result.data.token, { path: '/' })
    return ({ ...state, error: {}, cookie: false, signed: false, singUp: action.payload, isFetching: false })
  },
  [LOGIN_CODE_FAILURE]: (state, action) => {
    return ({ ...state, error: action.payload, cookie: false, signed: false, singUp: {}, isFetching: false })
  },
  [REG_CODE_REQUEST]: (state, action) => {
    return ({ ...state, error: {}, cookie: false, signed: false, regcode: {}, isFetching: true })
  },
  [REG_CODE_SUCCESS]: (state, action) => {
    return ({ ...state, error: {}, cookie: false, signed: false, regcode: action.payload, isFetching: false })
  },
  [REG_CODE_FAILURE]: (state, action) => {
    return ({ ...state, error: action.payload, cookie: false, signed: false, regcode: {}, isFetching: false })
  },
  [LOGIN_STATUS_REQUEST]: (state, action) => {
    return ({ ...state, error: {}, cookie: false, signed: {}, isFetching: true })
  },
  [LOGIN_STATUS_SUCCESS]: (state, action) => {
    return ({ ...state, error: {}, cookie: false, signed: action.payload, isFetching: false })
  },
  [LOGIN_STATUS_FAILURE]: (state, action) => {
    return ({ ...state, error: action.payload, cookie: false, signed: {}, isFetching: false })
  },
  [SINGOUT_REQUEST]: (state, action) => {
    return ({ ...state, error: {}, cookie: false, singout: {}, isFetching: true })
  },
  [SINGOUT_SUCCESS]: (state, action) => {
    cookie.remove('token', { path: '/' })
    return ({ ...state, error: {}, cookie: false, singout: action.payload, isFetching: false })
  },
  [SINGOUT_FAILURE]: (state, action) => {
    return ({ ...state, error: action.payload, cookie: false, singout: {}, isFetching: false })
  }
}

// Reducers
const initialState = { error: {}, signed: false, cookie: false, item: {}, regcode: {}, singUp: {}, singout: {}, isFetching: false }
export default function userReducer (state = initialState, action) {
  const handler = ACTION_HANDLERS[action.type]
  return handler ? handler(state, action) : state
}
